package com.atlin.project.service.impl;

import com.atlin.project.contant.CommonConstant;
import com.atlin.project.mapper.SysOperLogMapper;
import com.atlin.project.model.domain.SysOperLog;
import com.atlin.project.model.dto.sysOperLog.SysOperLogQueryRequest;
import com.atlin.project.model.vo.SysOperLogVO;
import com.atlin.project.service.SysOperLogService;
import com.atlin.project.utils.sql.SqlUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author atlin
 * @description sysOperLog
 * @createDate date
 */
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog>
        implements SysOperLogService {

    @Override
    public void valid(SysOperLog sysOperLog, boolean add) {

    }

    @Override
    public SysOperLogVO getSysOperLogVO(SysOperLog sysOperLog, HttpServletRequest request) {
        return SysOperLogVO.objToVo(sysOperLog);
    }

    /**
     * 获取查询包装类（用户根据哪些字段查询，根据前端传来的请求对象，得到 mybatis 框架支持的查询 QueryWrapper 类）
     *
     * @param sysOperLogQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<SysOperLog> getQueryWrapper(SysOperLogQueryRequest sysOperLogQueryRequest) {
        QueryWrapper<SysOperLog> queryWrapper = new QueryWrapper<>();
        if (sysOperLogQueryRequest == null) {
            return queryWrapper;
        }
        Long id = sysOperLogQueryRequest.getOperId();
        String title = sysOperLogQueryRequest.getTitle();
        String errorMsg = sysOperLogQueryRequest.getErrorMsg();
        String sortField = sysOperLogQueryRequest.getSortField();
        String sortOrder = sysOperLogQueryRequest.getSortOrder();

        queryWrapper.like(StringUtils.isNotBlank(title), "title", title);
        queryWrapper.like(StringUtils.isNotBlank(errorMsg), "errorMsg", errorMsg);
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);

        return queryWrapper;
    }

    @Override
    public Page<SysOperLogVO> getSysOperLogVOPage(Page<SysOperLog> sysOperLogPage, HttpServletRequest request) {
        List<SysOperLog> sysOperLogList = sysOperLogPage.getRecords();
        Page<SysOperLogVO> sysOperLogVOPage = new Page<>(sysOperLogPage.getCurrent(), sysOperLogPage.getSize(), sysOperLogPage.getTotal());
        if (CollectionUtils.isEmpty(sysOperLogList)) {
            return sysOperLogVOPage;
        }

        // 填充信息
        List<SysOperLogVO> sysOperLogVOList = sysOperLogList.stream().map(SysOperLogVO::objToVo).collect(Collectors.toList());
        sysOperLogVOPage.setRecords(sysOperLogVOList);
        return sysOperLogVOPage;
    }
}




